#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 10010;
int e[N], ne[N], h[N], idx;
int n, root;
int dp[N];
bool flag[N];
int neNode[N];

void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void dfs(int u)
{
    dp[u] = 1;
    int maxLen = 0, pos = -1;
    for(int i = h[u]; ~i; i = ne[i])
    {
        int nd = e[i];
        dfs(nd);
        if(dp[nd] > maxLen || (dp[nd] == maxLen && nd < pos))
            maxLen = dp[nd], pos = nd;
    }
    dp[u] += maxLen;
    neNode[u] = pos;
}

int main()
{
    memset(h, -1, sizeof h);
    memset(neNode, -1, sizeof neNode);
    cin >> n;
    for(int i = 0; i < n; ++i)
    {
        int k;
        cin >> k;
        for(int j = 0; j < k; ++j)
        {
            int tmp; cin >> tmp;
            add(i, tmp);
            flag[tmp] = true;
        }
    }
    for(int i = 0; i < n; ++i)
        if(!flag[i])
            root = i;
    dfs(root);
    cout << dp[root] << endl;
    int pos = root;
    while(~pos)
    {
        cout << pos << ' ';
        pos = neNode[pos];
    }
    cout << endl;
    // for(int i = h[0]; ~i; i = ne[i]) cout << e[i] << ' ';
    // cout << endl;
    return 0;
}